package tech.aistar.dao.impl;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import tech.aistar.dao.ICustomerDao;
import tech.aistar.entity.Customer;
import tech.aistar.util.MybatisUtil;

import java.io.InputStream;
import java.util.List;

/**
 * @author success
 * @version 1.0
 * @description:本类用来演示:
 * @date 2019/8/21 0021
 */
public class CustomerDaoImpl implements ICustomerDao{
    @Override
    public List<Customer> findAll() {
        //SqlSessionFactory - 重量级的对象 - 创建和销毁的成本比较高 - 一个数据对应一个SqlSessionFactory对象
        //作用 - 是用来负责创建SqlSession对象

        //1. 推荐mybatis-config.xml文件在src的根目录下
        String path = "mybatis-config.xml";

        //2. 获取配置文件的字节输入流
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);

        //3. 通过SqlSessionFactoryBuilder来构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);

        //System.out.println(sqlSessionFactory);

        //4. 通过SqlSessionFactory = 数据源 - 获取SqlSession对象
        //SqlSession = Connection + Cache[一级缓存]
        // System.out.println(sqlSessionFactory.openSession());

        SqlSession sqlSession = sqlSessionFactory.openSession();
        //传统的写法 - selectOne和selectList - 以后都是不推荐使用的 - 仍然是将sql硬编码到程序代码中
        //selectOne返回单个对象 - selectList就是返回多个对象

        //优势 - 实现了将查询的结果自动封装到java的对象

        //需要文件mybatis的映射文件.
        //所有的CRUD操作都是SqlSession对象.
        List<Customer> customerList = sqlSession.selectList("success.findAll");

       // customerList.forEach(System.out::println);

        //关闭
        sqlSession.close();

        return customerList;
    }

    @Override
    public void delById(Integer id) {
        String path = "mybatis-config.xml";
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);

        SqlSession sqlSession = sqlSessionFactory.openSession();

        //调用映射文件中的delete操作
        //缺陷在哪里namespace.sql标签的id硬编码到程序代码中 - 容易写错 - 不容易维护的
        sqlSession.delete("success.delById",id);

        //提交
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void save(Customer c) {
//        String path = "mybatis-config.xml";
//        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
//        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//
//        SqlSession sqlSession = sqlSessionFactory.openSession();
//
//        sqlSession.insert("success.save",c);
//
//        sqlSession.commit();
//        sqlSession.close();

        //1 - 获取SqlSession
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        sqlSession.insert("success.save",c);

        System.out.println("刚刚插入的主键的id的值:"+c.getId());

        sqlSession.commit();

        MybatisUtil.close(sqlSession);
    }

    @Override
    public List<Customer> findAllByLike(String cname) {

        SqlSession sqlSession = MybatisUtil.getSqlSession();

        List<Customer> customers = sqlSession.selectList("success.findAllByLike",cname);

        sqlSession.commit();

        sqlSession.close();

        return customers;
    }

    @Override
    public void update(Customer customer) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        sqlSession.update("success.update",customer);

        sqlSession.commit();

        sqlSession.close();
    }

    @Override
    public Customer getById(Integer id) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();

        //返回的是单个对象
        Customer customer = sqlSession.selectOne("success.getById",id);

        sqlSession.commit();

        sqlSession.close();

        return customer;
    }
}
